/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace ELE.EnterpriseLibrary.Data.Configuration
{
  /// <summary>
  /// Configuration information of a mapped relational table.
  /// </summary>
  public class ObjectTableData : NamedConfigurationElement
  {
    private const string jointColumnProperty = "jointColumn";
    private const string bindingsProperty = "bindings";
    /// <summary>
    /// 
    /// </summary>
    public ObjectTableData() { }
    /// <summary>
    /// Contructor that takes the name of the table
    /// </summary>
    /// <param name="name"></param>
    public ObjectTableData(string name) : base(name) { }
    /// <summary>
    /// In the case of non-primary tables, this defines the name of the column that is joined to
    /// the mapping's primary key of the primary table.
    /// <para>This property is not required for primary tables.</para>
    /// <para>Example:</para>
    /// <code>
    /// &lt;tables>
    /// &lt;add name="employee">
    ///   &lt;bindings>
    ///     &lt;add name="Id" property="Id" sqlType="INT" inName="@Id" outName="id" column="id" primaryKey="true"/>
    ///     &lt;add name="Name" property="Name" sqlType="VARCHAR" inName="@Name" outName="name" column="name" />
    ///     &lt;add name="Address" property="Address" refType="Address" outName="addressId" inName="@AddressId" column="addressId" />
    ///     &lt;add name="Aliases" property="Aliases" refType="Alias" collection="true" jointBinding="EmployeeId"/>
    ///     &lt;add name="Equipment" property="Equipment" refType="Equipment" collection="true" inName="@EmployeeId" jointColumn="employeeId"/>
    ///   &lt;/bindings>
    /// &lt;/add>
    /// &lt;add name="employee_extra" jointColumn="employeeId">
    ///   &lt;bindings>
    ///     &lt;add name="Age" property="Age" sqlType="INT" inName="@Age" outName="age" column="age"/>
    ///     &lt;add name="BirthDate" property="BirthDate" sqlType="DATETIME" inName="@BirthDate" outName="birthDate" column="birthDate" nullableValue="datetime.minvalue"/>
    ///   &lt;/bindings>
    /// &lt;/add>
    /// &lt;/tables>
    /// </code>
    /// <para>In this case, records in the employee_extra table are matched to records in the employee table by 
    /// pairing employee_extra.employeeId with employee.id, since the binding for column id in the employee
    /// table is defined as its primary key.</para>
    /// </summary>
    [ConfigurationProperty(jointColumnProperty, IsRequired = false)]
    public string JointColumn
    {
      get { return (string)base[jointColumnProperty]; }
      set { base[jointColumnProperty] = value; }
    }
    /// <summary>
    /// A set of bindings that relate properties of the mappe object to the defined table's columns.
    /// </summary>
    [ConfigurationProperty(bindingsProperty, IsRequired = true)]
    public NamedElementCollection<PropertyBindingData> Bindings
    {
      get
      {
        return (NamedElementCollection<PropertyBindingData>)base[bindingsProperty];
      }
    }

  }
}
